package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factorydata.domain.InventoryReceiptInfo;
import com.ruoyi.factorydata.domain.MaterialRequisition;
import com.ruoyi.factorydata.domain.ModelInventoryFlow;
import com.ruoyi.factorydata.domain.WorkOrderInfo;
import com.ruoyi.factorydata.mapper.InventoryReceiptInfoMapper;
import com.ruoyi.factorydata.mapper.MaterialRequisitionMapper;
import com.ruoyi.factorydata.mapper.ModelInventoryFlowMapper;
import com.ruoyi.factorydata.mapper.WorkOrderInfoMapper;
import com.ruoyi.factorydata.service.IModelInventoryFlowService;
import com.ruoyi.factorydata.service.IWarehouseInfoService;
import com.ruoyi.factorydata.service.IWarehousePositionService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * 母版库存流水Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class ModelInventoryFlowServiceImpl extends ServiceImpl<ModelInventoryFlowMapper, ModelInventoryFlow> implements IModelInventoryFlowService {
    private final ModelInventoryFlowMapper modelInventoryFlowMapper;
    private final IWarehouseInfoService warehouseInfoService;
    private final IWarehousePositionService warehousePositionService;
    private final InventoryReceiptInfoMapper inventoryReceiptInfoMapper;
    private final MaterialRequisitionMapper materialRequisitionMapper;
    private final WorkOrderInfoMapper workOrderInfoMapper;

    public ModelInventoryFlowServiceImpl(ModelInventoryFlowMapper modelInventoryFlowMapper,
                                         IWarehouseInfoService warehouseInfoService,
                                         IWarehousePositionService warehousePositionService,
                                         InventoryReceiptInfoMapper inventoryReceiptInfoMapper,
                                         MaterialRequisitionMapper materialRequisitionMapper,
                                         WorkOrderInfoMapper workOrderInfoMapper) {
        this.modelInventoryFlowMapper = modelInventoryFlowMapper;
        this.warehouseInfoService = warehouseInfoService;
        this.warehousePositionService = warehousePositionService;
        this.inventoryReceiptInfoMapper = inventoryReceiptInfoMapper;
        this.materialRequisitionMapper = materialRequisitionMapper;
        this.workOrderInfoMapper = workOrderInfoMapper;
    }

    /**
     * 查询母版库存流水
     *
     * @param id 母版库存流水主键
     * @return 母版库存流水
     */
    @Override
    public ModelInventoryFlow selectModelInventoryFlowById(Long id) {
        return modelInventoryFlowMapper.selectModelInventoryFlowById(id);
    }

    /**
     * 查询母版库存流水列表
     *
     * @param modelInventoryFlow 母版库存流水
     * @return 母版库存流水
     */
    @Override
    public List<ModelInventoryFlow> selectModelInventoryFlowList(ModelInventoryFlow modelInventoryFlow) {
        List<ModelInventoryFlow> modelInventoryFlows = modelInventoryFlowMapper.selectModelInventoryFlowList(modelInventoryFlow);
        HashMap<Long, String> positionNameCache = new HashMap<>();
        HashMap<Long, String> warehouseNameCache = new HashMap<>();

        modelInventoryFlows.forEach(inventoryFlow -> {
            String positionName = warehousePositionService.getByIdAndCache(inventoryFlow.getWarehousePositionId(), positionNameCache);
            String warehouseName = warehouseInfoService.getNameByIdAndCache(inventoryFlow.getWarehouseId(), warehouseNameCache);
            if(StringUtils.equals(inventoryFlow.getDocumentType(), "modelIn")){
                InventoryReceiptInfo receiptInfo = inventoryReceiptInfoMapper.selectOne(new LambdaQueryWrapper<InventoryReceiptInfo>()
                        .eq(InventoryReceiptInfo::getReceiptNumber, inventoryFlow.getDocumentNumber())
                        .last("limit 1"));
                inventoryFlow.setDocumentId(Optional.ofNullable(receiptInfo).map(InventoryReceiptInfo::getId).orElse(null)+"");
            } else if (StringUtils.equals(inventoryFlow.getDocumentType(), "requisitionOut")
                    ||StringUtils.equals(inventoryFlow.getDocumentType(), "requisition_return_in")) {
                MaterialRequisition materialRequisition = materialRequisitionMapper.selectOne(new LambdaQueryWrapper<MaterialRequisition>()
                        .eq(MaterialRequisition::getRequisitionNo, inventoryFlow.getDocumentNumber())
                        .last("limit 1"));
                inventoryFlow.setDocumentId(Optional.ofNullable(materialRequisition).map(MaterialRequisition::getId).orElse(null)+"");
            }else if (StringUtils.equals(inventoryFlow.getDocumentType(), "product_model")
                    ||StringUtils.equals(inventoryFlow.getDocumentType(), "product_in")
                    ||StringUtils.equals(inventoryFlow.getDocumentType(), "model_process_again")) {
                WorkOrderInfo workOrderInfo = workOrderInfoMapper.selectOne(new LambdaQueryWrapper<WorkOrderInfo>()
                        .eq(WorkOrderInfo::getWorkOrderCode, inventoryFlow.getDocumentNumber())
                        .last("limit 1"));
                inventoryFlow.setDocumentId(Optional.ofNullable(workOrderInfo).map(WorkOrderInfo::getId).orElse(null)+"");
            }
            inventoryFlow.setWarehousePositionName(positionName);
            inventoryFlow.setWarehouseName(warehouseName);

        });
        return modelInventoryFlows;
    }

    /**
     * 新增母版库存流水
     *
     * @param modelInventoryFlow 母版库存流水
     * @return 结果
     */
    @Override
    public int insertModelInventoryFlow(ModelInventoryFlow modelInventoryFlow) {
                modelInventoryFlow.setCreateTime(DateUtils.getNowDate());
            return modelInventoryFlowMapper.insertModelInventoryFlow(modelInventoryFlow);
    }

    /**
     * 修改母版库存流水
     *
     * @param modelInventoryFlow 母版库存流水
     * @return 结果
     */
    @Override
    public int updateModelInventoryFlow(ModelInventoryFlow modelInventoryFlow) {
                modelInventoryFlow.setUpdateTime(DateUtils.getNowDate());
        return modelInventoryFlowMapper.updateModelInventoryFlow(modelInventoryFlow);
    }

    /**
     * 批量删除母版库存流水
     *
     * @param ids 需要删除的母版库存流水主键
     * @return 结果
     */
    @Override
    public int deleteModelInventoryFlowByIds(Long[] ids) {
        return modelInventoryFlowMapper.deleteModelInventoryFlowByIds(ids);
    }

    /**
     * 删除母版库存流水信息
     *
     * @param id 母版库存流水主键
     * @return 结果
     */
    @Override
    public int deleteModelInventoryFlowById(Long id) {
        return modelInventoryFlowMapper.deleteModelInventoryFlowById(id);
    }
}